/**
 * 
 */
package co.com.umobile.dao;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import co.com.umobile.domain.Place;
import co.com.umobile.persistence.DatabaseManager;


/**
 * @since 26/07/2012
 */
public class SubjectDetailDao implements GenericDao {
	
	
	/**
	   * gets the Place by code.
	   * 
	   * @param subjectDetailCode subjectDetailCode.
	   * @return List<Place>
	   */
	public static List<Place> getPlaceBySubjectDetail(String subjectDetailCode) {
		
		String[] values = { subjectDetailCode };
		

//		/** Script to create schedule table. */
//		public static final String SUBJECT_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS Subject (subjectCode text PRIMARY KEY, "
//				+ " subjectName text,teacherCode text, teacherName text, subjectPeriod text, grade1 real, "
//				+ " grade2 real, grade3 real, grade4 real, finalGrade real, definitiveGrade real)";	
//		
//		/** Script to create place table. */
//		public static final String PLACE_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS Place ( "
//				+ " placeCode text PRIMARY KEY, placeName text, placeDescription text, room text, block text, placeType integer, placeTypeDescription text, longitude real, latitude real)";
//
//		/** Script to create schedule detail table. */
//		public static final String SUBJECT_DETAIL_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS SubjectDetail ( "
//				+ " subjectDetailCode text PRIMARY KEY , subjectCode text REFERENCES Subject, subjectDate text, subjectTime text, placeCode text REFERENCES Place)";

		
		String text = "Select s.placeCode, placeName, placeDescription, room, block, placeType, placeTypeDescription, longitude, latitude, " 
				+"subjectDate, subjectTime, subjectName, teacherName from Subject sb, SubjectDetail s, Place p "
				+"WHERE sb.subjectCode = s.subjectCode AND s.placeCode = p.placeCode AND s.subjectDetailCode = ? ";
		
	    Cursor cursor = DatabaseManager.getDatabase().rawQuery(text,values);
	    
	    List<Place> list = new ArrayList<Place>();

	    while (cursor.moveToNext()) {
	      list.add(convertCursorToPlace(cursor));
	    }
	    cursor.close();
	    return list;
	}
	
	/**
	   * convert a cursor into a Place object.
	   * 
	   * @param cursor cursor.
	   * @return Place
	   */
	  private static Place convertCursorToPlace(Cursor cursor) {

	    Place place = new Place(
	    		cursor.getString(0), 
	    		cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),
	    		cursor.getInt(5), cursor.getString(6), cursor.getDouble(7), cursor.getDouble(8),
	    		cursor.getString(9),cursor.getString(10),cursor.getString(11),cursor.getString(12));
	    
	    return place;
	  }

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#insertOrUpdateData(java.lang.String, int)
	 */
	public void insertOrUpdateData(String values, int serviceNumber) {
		 String sqlText = "insert or replace into SubjectDetail(subjectDetailCode, subjectCode, subjectDate, subjectTime, placeCode) values(" + values + ")";
		 DatabaseManager.getDatabase().execSQL(sqlText);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#deleteDataMirror()
	 */
	public void deleteDataMirror() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#copyDataToMirror()
	 */
	public void copyDataToMirror() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#deleteDataOriginal()
	 */
	public void deleteDataOriginal() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see co.com.umobile.dao.GenericDao#copyDataToOriginal()
	 */
	public void copyDataToOriginal() {
		// TODO Auto-generated method stub

	}

}
